﻿using System;
using System.Data.SqlClient;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using GE_Database_Manager;
using System.Collections.Generic;


namespace UnitTests
{
	[TestClass]
	public class Standardized_Wrapper
	{

        [TestMethod]
        public void ErrorLog() { 
            ErrorLog log =  new ErrorLog();
            log.writeToLog("ADAPT","Unable to insert stuff");
        
        }

		[TestMethod]
		public void InsertMachine()
		{
			SqlConnection connection = new SqlConnection();
			connection.ConnectionString = ("Server=.\\V2012;Integrated Security=true");
			GE_Database_Manager.Standardized_Wrapper sw = new GE_Database_Manager.Standardized_Wrapper(connection, true, "GE_Standardized");

			int machineId = sw.InsertMachine("FakeSerial", "gearbox", "reno", "RNO-FAKE", "#2345", "General Electric");
			Assert.AreEqual(machineId, 1);

			machineId = sw.InsertMachine("FakeSerial", "gearbox", "reno", "RNO-FAKE", "#2345", "General Electric");
			Assert.AreEqual(machineId, 1);
		}

		[TestMethod]
		public void InsertStages()
		{
			SqlConnection connection = new SqlConnection();
			connection.ConnectionString = ("Server=.\\V2012;Integrated Security=true");
			GE_Database_Manager.Standardized_Wrapper sw = new GE_Database_Manager.Standardized_Wrapper(connection, false, "GE_Standardized");

			int stage1 = sw.InsertStage(Designation.Parallel, 1, null, 15, 50, null, null, null, null);
			Assert.AreEqual(1, stage1);			// should be the first one in the DB
			
			int stage2 = sw.InsertStage(Designation.Parallel, 1, null, 15, 50, null, null, null, null);
			Assert.AreEqual(stage1, stage2);	// duplicates, should point to the same stage_id


			int stage3 = sw.InsertStage(Designation.Planetary, 1, Fixed.Planet, null, null, 50, 30, 15, 3);
			Assert.AreEqual(stage3, 2);			// should be the second one in the DB

			int stage4 = sw.InsertStage(Designation.Planetary, 1, Fixed.Planet, null, null, 50, 30, 15, 3);
			Assert.AreEqual(stage3, stage4);	// duplicates, should point to the same stage_id*/
		}

		[TestMethod]
		public void InsertSensor()
		{
			SqlConnection connection = new SqlConnection();
			connection.ConnectionString = ("Server=.\\V2012;Integrated Security=true");
			GE_Database_Manager.Standardized_Wrapper sw = new GE_Database_Manager.Standardized_Wrapper(connection, false, "GE_Standardized");

			int sensor1 = sw.InsertSensor("FakeSensor1", 1);
			Assert.AreEqual(1, sensor1);

			int sensor2 = sw.InsertSensor("FakeSensor1", 1);
			Assert.AreEqual(sensor1, sensor2);

			int sensor3 = sw.InsertSensor("FakeSensor2", 1);
			Assert.AreEqual(2, sensor3);

			int sensor4 = sw.InsertSensor("FakeSensor2", 1);
			Assert.AreEqual(sensor3, sensor4);

		}

		[TestMethod]
		public void InsertMachineCondition()
		{
			SqlConnection connection = new SqlConnection();
			connection.ConnectionString = ("Server=.\\V2012;Integrated Security=true");
			GE_Database_Manager.Standardized_Wrapper sw = new GE_Database_Manager.Standardized_Wrapper(connection, false, "GE_Standardized");

			bool inserted = sw.InsertMachineCondition(1, new DateTime(2014, 1, 1, 0, 0, 0), new DateTime(2014, 12, 31, 23, 59, 59), "Cracked Bearing", "Comments", "System1");
			Assert.AreEqual(true, inserted);
		}

		[TestMethod]
		public void InsertWaveform()
		{
			SqlConnection connection = new SqlConnection();
			connection.ConnectionString = ("Server=.\\V2012;Integrated Security=true");
			GE_Database_Manager.Standardized_Wrapper sw = new GE_Database_Manager.Standardized_Wrapper(connection, false, "GE_Standardized");

			Single sample = 1.235F;
			Byte[] status = BitConverter.GetBytes(0F);

			bool insert1 = sw.InsertWaveform(1, new DateTime(2014, 2, 28, 12, 47, 0), 1200, 6400, 32, BitConverter.GetBytes(sample), sw.GetEngrUnitIdFromAbbreviation("mm/s"), status);
			bool insert2 = sw.InsertWaveform(1, new DateTime(2014, 2, 28, 12, 47, 0), 1200, 6400, 32, BitConverter.GetBytes(sample), sw.GetEngrUnitIdFromAbbreviation("mm/s"), status);
			Assert.AreEqual(false, insert2);

			bool insert3 = sw.InsertWaveform(1, new DateTime(2014, 2, 28, 12, 47, 10), 1200, 6400, 32, BitConverter.GetBytes(sample), sw.GetEngrUnitIdFromAbbreviation("mm/s"), status);
			Assert.AreEqual(true, insert3);
		}

		[TestMethod]
		public void TestStoredProcedure_GetEngrUnitFromAbbrev()
		{
			SqlConnection connection = new SqlConnection();
			connection.ConnectionString = ("Server=.\\V2012;Integrated Security=true");
			GE_Database_Manager.Standardized_Wrapper sw = new GE_Database_Manager.Standardized_Wrapper(connection, false, "GE_Standardized");

			Assert.AreEqual(sw.GetEngrUnitIdFromAbbreviation("FakeUnit"), -1);
			Assert.AreEqual(sw.GetEngrUnitIdFromAbbreviation("mm"), 232);
		}
	}
}
